package com.name.givename.controller;

import com.name.givename.dto.ShareDTO;
import com.name.givename.entity.WeiXinEntity;
import com.name.givename.enums.ResultEnum;
import com.name.givename.exception.ServiceException;
import com.name.givename.param.ShareParams;
import com.name.givename.result.ResultVoUtils;
import com.name.givename.utils.Result;
import com.name.givename.utils.WeXinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by yangtianrui on 18/9/3.
 */
@RequestMapping("/share")
@RestController
@Api("微信分享")
@Slf4j
public class ShareController {


    @ApiOperation("微信分享")
    @PostMapping("/share")
    public Result<ShareDTO> wechatShare(@RequestBody ShareParams shareParams) {

        if (shareParams.getShareUrl() == null) {
            throw new ServiceException(ResultEnum.SHARE_URL_NULL.getCode(),ResultEnum.SHARE_URL_NULL.getValue());
        }
        WeiXinEntity weiXinEntity = WeXinUtil.getWeiXinEntity(shareParams.getShareUrl());
        ShareDTO shareDTO = new ShareDTO();
        shareDTO.setJsTicket(weiXinEntity.getTicket());
        shareDTO.setNonceStr(weiXinEntity.getNoncestr());
        shareDTO.setTimeStemp(weiXinEntity.getTime_stamp());
        shareDTO.setSignatrue(weiXinEntity.getSignature());
        return ResultVoUtils.success(shareDTO);
    }


}
